package com.ztorn.controller;

import com.ztorn.dtos.*;
import com.ztorn.model.User;
import com.ztorn.services.OutService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api")
@Slf4j
public class OutboundController {

    @Autowired
    private OutService outService;

    @RequestMapping("/pick")
    public ApiResult pick(@RequestBody PickReqDTO pickDTO) {
        if(Objects.isNull(pickDTO) || StringUtils.isEmpty(pickDTO.getWarehouse())){
            return ApiResult.fail("410","warehouse不能为空");
        }
        List<PickRespDTO> mypktHdrs = outService.selectOrderByWhse(pickDTO.getWarehouse());
        if(CollectionUtils.isEmpty(mypktHdrs)){
            return ApiResult.fail("411","没有拣货单");
        }
        return ApiResult.ok(mypktHdrs);
    }

    @RequestMapping("/pickDetail")
    public ApiResult pickDetail(@RequestBody PickReqDTO pickReqDTO){
        return ApiResult.ok(outService.selectPickDetail(pickReqDTO.getId(),pickReqDTO.getWarehouse()));
    }

    @RequestMapping("/allowPick")
    public ApiResult allowPick(@RequestBody PickReqDTO pickReqDTO){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        User user = (User)principal;
        if(StringUtils.isEmpty(pickReqDTO.getPickMan())){
            outService.updatePickMan(user.getAccount(),pickReqDTO.getId(),pickReqDTO.getWarehouse());
            return ApiResult.ok();
        }else{
            if(user.getAccount().equalsIgnoreCase(pickReqDTO.getPickMan())){
                return ApiResult.ok();
            }else{
                return ApiResult.fail("413","此单正在被"+pickReqDTO.getPickMan()+"拣货");
            }
        }
    }

    @RequestMapping("/pickCalculate")
    public ApiResult pickCalculate(@RequestBody PickReqDTO pickReqDTO){
        List<PickDetailRespDTO> pickDetailRespDTOS = outService.selectPickDetail(pickReqDTO.getId(), pickReqDTO.getWarehouse());
        return ApiResult.ok(pickDetailRespDTOS.get(0));
    }

    @RequestMapping("/picked")
    public ApiResult picked(@RequestBody UpdatePickedDTO updatePickedDTO){
        try{
            outService.updatePicked(updatePickedDTO.getSku(), updatePickedDTO.getPktCtrlNbr(), updatePickedDTO.getWhse());
            List<PickDetailRespDTO> pickDetailRespDTOS = outService.selectPickDetail(updatePickedDTO.getPktCtrlNbr(), updatePickedDTO.getWhse());
            return ApiResult.ok(pickDetailRespDTOS.get(0));
        }catch (Exception e){
            return ApiResult.fail("412","拣货失败");
        }
    }
}
